from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime
from core.base.orm_model import Base
from sqlalchemy.orm import relationship


class Role(Base):
    """角色模型"""
    __tablename__ = "roles"

    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50), unique=True, nullable=False, comment="角色名称")
    desc = Column(String(255), nullable=True, comment="角色描述")
    status = Column(Boolean, default=True, comment="状态：0-禁用，1-启用")
    create_time = Column(DateTime, default=datetime.now, comment="创建时间")
    update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")

    # 关联关系：角色-权限 多对多
    permissions = relationship("Permission", secondary="role_permissions", back_populates="roles")
    
    # 关联关系：角色-用户 多对多
    #users = relationship("User", secondary="user_roles", back_populates="roles")

    def __repr__(self):
        return f"<Role {self.name}>"